/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package simpleclient;

//import edu.ipfw.cs590.cheat.AccessType;
import edu.ipfw.cs590.cheat.AccessRights;
import edu.ipfw.cs590.cheat.AssignmentManagementInterface;
import edu.ipfw.cs590.cheat.DenyRegistrationRequest;
import edu.ipfw.cs590.cheat.DenyRegistrationRequestResponse;
import edu.ipfw.cs590.cheat.GetAccessRights;
import edu.ipfw.cs590.cheat.GetAccessRightsResponse;
import edu.ipfw.cs590.cheat.GetAssignmentHistory;
import edu.ipfw.cs590.cheat.GetAssignmentHistoryResponse;
import edu.ipfw.cs590.cheat.GetPendingRegistrationRequests;
import edu.ipfw.cs590.cheat.GetPendingRegistrationRequestsResponse;
import edu.ipfw.cs590.cheat.GetPerson;
import edu.ipfw.cs590.cheat.GetPersonResponse;
import edu.ipfw.cs590.cheat.GrantRegistrationRequest;
import edu.ipfw.cs590.cheat.GrantRegistrationRequestResponse;
import edu.ipfw.cs590.cheat.Logout;
import edu.ipfw.cs590.cheat.RegisteredPerson;
import edu.ipfw.cs590.cheat.ReplaceAssignment;
import edu.ipfw.cs590.cheat.ReplaceAssignmentResponse;
import edu.ipfw.cs590.cheat.SubmitAssignment;
import edu.ipfw.cs590.cheat.SubmitAssignmentResponse;
import edu.ipfw.cs590.cheat.UserRights;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPFaultException;

/**
 *
 * @author rrm46582
 */
public class Main {
    static List<RegisteredPerson> pendingUsers;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        String user = "r.r@m.com";
        String access = "INST";
        String admin = "sarcasm@sarcasm.org";
        String adminPwd = "sarcasm";
        try
        {
            while ( true )
            {
            // TODO code application logic here
            callRegistration(user, access);
            int sessId = callLogin(admin, adminPwd);
            callGetHistory(sessId, callSubmit(sessId));
            callGetPerson(sessId, admin);
            callGetAccessRights(sessId);
            callGetPendingRequests(sessId);
            //if ( pendingUsers.size() > 0)
            //    callGrant(sessId, user);
            callGetPendingRequests(sessId);
//            callRegistration(user, access);
//            callGetPendingRequests(sessId);
//            if ( pendingUsers.size() > 0)
//                callGrant(sessId, user);
//            callGetPendingRequests(sessId);
            callLogout(sessId);
            sessId = callLogin(user, user);
            callReplace(sessId, callSubmit(sessId));
            }

        } 
        catch (MalformedURLException ex)
        {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }



 private static String callSubmit(int sessId)
 {
    URL wsdlURL = null;
    String returnValue = "";
        {
            System.out.println("Submit: " + sessId);
            FileInputStream io = null;

            try {
                String token = Integer.toString(sessId);
                io = new FileInputStream("c:/Users/Public/a6.zip");
                File f = new File("c:/Users/Public/a6.zip");
                byte[] b = new byte[(int)f.length()];
                io.read(b);
                JAXBElement<byte[]> value = new JAXBElement(new QName( "Archive"), b.getClass(), b);
                wsdlURL = new URL("http://robinrmcomputer:8080/CheatApp-war/AssignmentManagementService?wsdl");
                QName serviceQName = new QName("HTTP://cheat.cs590.ipfw.edu", "AssignmentManagementService");
                QName portQName = new QName("HTTP://cheat.cs590.ipfw.edu", "AssignmentManagementSOAP");
                Service service = null;
                service = Service.create(wsdlURL, serviceQName);
                AssignmentManagementInterface jiPort = (AssignmentManagementInterface) service.getPort(portQName,AssignmentManagementInterface.class);
                //pendingUsers.get(3).setPassword("hello");
                SubmitAssignment submit = new SubmitAssignment();
                submit.setArchive(value);
                SubmitAssignmentResponse answer = jiPort.submitAssignment(submit, token);
                System.out.println("Access: " + answer.getResult());
                returnValue = answer.getResult();
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
            finally
            {
                try
                {
                    io.close();
                } 
                catch (IOException ex)
                {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
                return returnValue;
            }
        }

 }



   private static String callGetHistory(int sessId, String uuid)
  {
    URL wsdlURL = null;
    FileInputStream io = null;
    System.out.print("GetHistory: " + "session = " + sessId + " UUID= " + uuid);

            try
            {
                String token = Integer.toString(sessId);
                wsdlURL = new URL("http://robinrmcomputer:8080/CheatApp-war/AssignmentManagementService?wsdl");
                QName serviceQName = new QName("HTTP://cheat.cs590.ipfw.edu", "AssignmentManagementService");
                QName portQName = new QName("HTTP://cheat.cs590.ipfw.edu", "AssignmentManagementSOAP");
                Service service = null;
                service = Service.create(wsdlURL, serviceQName);
                AssignmentManagementInterface jiPort = (AssignmentManagementInterface) service.getPort(portQName,AssignmentManagementInterface.class);
                //pendingUsers.get(3).setPassword("hello");
                GetAssignmentHistory history = new GetAssignmentHistory();
                history.setUUID(uuid);

                GetAssignmentHistoryResponse answer = jiPort.getAssignmentHistory(history, token);
                System.out.println("History Length: " + answer.getHistoryResult().size());
                return "42";
            }
            catch (IOException ex)
            {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
            finally
            {
                try
                {
                    io.close();
                }
                catch (IOException ex)
                {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
                return "";
            }
        }


  private static String callReplace(int sessId, String uuid)
  {
    URL wsdlURL = null;
    FileInputStream io = null;
    System.out.print("Replace: " + "session = " + sessId + " UUID= " + uuid);

            try
            {
                String token = Integer.toString(sessId);
                io = new FileInputStream("c:/Users/Public/a6.zip");
                File f = new File("c:/Users/Public/a6.zip");
                byte[] b = new byte[(int)f.length()];
                io.read(b);
                JAXBElement<byte[]> value = new JAXBElement(new QName( "Archive"), b.getClass(), b);
                wsdlURL = new URL("http://robinrmcomputer:8080/CheatApp-war/AssignmentManagementService?wsdl");
                QName serviceQName = new QName("HTTP://cheat.cs590.ipfw.edu", "AssignmentManagementService");
                QName portQName = new QName("HTTP://cheat.cs590.ipfw.edu", "AssignmentManagementSOAP");
                Service service = null;
                service = Service.create(wsdlURL, serviceQName);
                AssignmentManagementInterface jiPort = (AssignmentManagementInterface) service.getPort(portQName,AssignmentManagementInterface.class);
                //pendingUsers.get(3).setPassword("hello");
                ReplaceAssignment submit = new ReplaceAssignment();
                submit.setUUID(uuid);
                submit.setArchive(value);

                submit.setArchive(value);
                ReplaceAssignmentResponse answer = jiPort.replaceAssignment(submit, token);
                System.out.println("Access: " + answer.getResult());
                return answer.getResult();
            } 
            catch (IOException ex)
            {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
            finally
            {
                try
                {
                    io.close();
                } 
                catch (IOException ex)
                {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
                return "";
            }
        }


 private static void callDeny(int sessId, String user)
 {
    URL wsdlURL = null;
    System.out.println("Deny: " + sessId + " " + user);
        try
		{
			wsdlURL = new URL("http://robinrmcomputer:8080/CheatApp-war/AssignmentManagementService?wsdl");
            QName serviceQName = new QName("HTTP://cheat.cs590.ipfw.edu","AssignmentManagementService");
			QName portQName =    new QName("HTTP://cheat.cs590.ipfw.edu","AssignmentManagementSOAP");
            Service service = null;
            service = Service.create(wsdlURL, serviceQName);

            AssignmentManagementInterface jiPort =
                    (AssignmentManagementInterface)  service.getPort(portQName,AssignmentManagementInterface.class);

            //pendingUsers.get(3).setPassword("hello");
            DenyRegistrationRequest deny = new DenyRegistrationRequest();
            deny.setPendingUser(user);
            DenyRegistrationRequestResponse answer = jiPort.denyRegistrationRequest(deny, new Integer(sessId).toString());

            System.out.println("Access: " + answer.isDenyResult());
		}
		catch (MalformedURLException e)
		{
			e.printStackTrace();
		}
 }


 private static void callGetPerson(int sessId, String user)
 {
    URL wsdlURL = null;
    System.out.println("GetPerson: " + sessId + " " + user);
        try
		{
			wsdlURL = new URL("http://robinrmcomputer:8080/CheatApp-war/AssignmentManagementService?wsdl");
            QName serviceQName = new QName("HTTP://cheat.cs590.ipfw.edu","AssignmentManagementService");
			QName portQName =    new QName("HTTP://cheat.cs590.ipfw.edu","AssignmentManagementSOAP");
            Service service = null;
            service = Service.create(wsdlURL, serviceQName);

            AssignmentManagementInterface jiPort =
                    (AssignmentManagementInterface)  service.getPort(portQName,AssignmentManagementInterface.class);


            GetPerson person = new GetPerson();
        //    person.setEMAIL(user);
        //    GetPersonRequest grant = new GrantRegistrationRequest();

          //  grant.setPendingUser(user);
            GetPersonResponse answer = jiPort.getPerson(person, new Integer(sessId).toString());

            System.out.println("Access: " + answer.getPerson().getAffiliation());
		}
		catch (MalformedURLException e)
		{
			e.printStackTrace();
		}
 }


  private static void callGrant(int sessId, String user)
 {
    URL wsdlURL = null;
    System.out.println("GetGrant: " + sessId + " " + user);
        try
		{
			wsdlURL = new URL("http://robinrmcomputer:8080/CheatApp-war/AssignmentManagementService?wsdl");
            QName serviceQName = new QName("HTTP://cheat.cs590.ipfw.edu","AssignmentManagementService");
			QName portQName =    new QName("HTTP://cheat.cs590.ipfw.edu","AssignmentManagementSOAP");
            Service service = null;
            service = Service.create(wsdlURL, serviceQName);

            AssignmentManagementInterface jiPort =
                    (AssignmentManagementInterface)  service.getPort(portQName,AssignmentManagementInterface.class);


            GrantRegistrationRequest grant = new GrantRegistrationRequest();

            grant.setPendingUser(user);
            GrantRegistrationRequestResponse answer = jiPort.grantRegistrationRequest(grant, new Integer(sessId).toString());

            System.out.println("Access: " + answer.isGrantResult());
		}
		catch (MalformedURLException e)
		{
			e.printStackTrace();
		}
 }



 private static int callLogin(String user, String pwd) throws MalformedURLException
    {

        URL wsdlURL = null;
        System.out.println("Login: " + user + "/" + pwd);
		try
		{
			wsdlURL = new URL("http://robinrmcomputer:8080/CheatApp-war/AssignmentManagementService?wsdl");
            QName serviceQName = new QName("HTTP://cheat.cs590.ipfw.edu","AssignmentManagementService");
			QName portQName =    new QName("HTTP://cheat.cs590.ipfw.edu","AssignmentManagementSOAP");
            Service service = null;
            service = Service.create(wsdlURL, serviceQName);

            AssignmentManagementInterface jiPort =
                    (AssignmentManagementInterface)  service.getPort(portQName,AssignmentManagementInterface.class);



            //String answer = jiPort.login("sarcasm@sarcasm.org", "sarcasm");
            //String answer = jiPort.login("sarcasm@sarcasm.org", "sarcasm");
            String answer = jiPort.login(user, pwd);
            System.out.println("Login session id: " + answer);
            return new Integer(answer);
		}
		catch (MalformedURLException e)
		{
			e.printStackTrace();
            return -1;
		}

    }





 private static void callLogout(int sessId) throws MalformedURLException
 {

    URL wsdlURL = null;
    System.out.println("Logout: " + sessId);
        try
		{
			wsdlURL = new URL("http://robinrmcomputer:8080/CheatApp-war/AssignmentManagementService?wsdl");
            QName serviceQName = new QName("HTTP://cheat.cs590.ipfw.edu","AssignmentManagementService");
			QName portQName =    new QName("HTTP://cheat.cs590.ipfw.edu","AssignmentManagementSOAP");
            Service service = null;
            service = Service.create(wsdlURL, serviceQName);

            AssignmentManagementInterface jiPort =
                    (AssignmentManagementInterface)  service.getPort(portQName,AssignmentManagementInterface.class);


			Logout lo = new Logout();
            jiPort.logout(lo, new Integer(sessId).toString());
            //System.out.println("Logout: " + answer.getLogoutResult());
		}
		catch (MalformedURLException e)
		{
			e.printStackTrace();
		}
	}


  private static void callGetAccessRights(int sessId) throws MalformedURLException
  {

    URL wsdlURL = null;
    System.out.println("AccessRights: " + sessId);

        try
		{
			wsdlURL = new URL("http://robinrmcomputer:8080/CheatApp-war/AssignmentManagementService?wsdl");
            QName serviceQName = new QName("HTTP://cheat.cs590.ipfw.edu","AssignmentManagementService");
			QName portQName =    new QName("HTTP://cheat.cs590.ipfw.edu","AssignmentManagementSOAP");
            Service service = null;
            service = Service.create(wsdlURL, serviceQName);

            AssignmentManagementInterface jiPort =
                    (AssignmentManagementInterface)  service.getPort(portQName,AssignmentManagementInterface.class);


			GetAccessRights ar = new GetAccessRights();
            GetAccessRightsResponse answer = jiPort.getAccessRights(ar, new Integer(sessId).toString());

            System.out.println("Access: " + answer.getAccessRightsResult().getRights());
		}
		catch (MalformedURLException e)
		{
			e.printStackTrace();
		}
	}


  private static void callGetPendingRequests(int sessId) throws MalformedURLException
  {

    URL wsdlURL = null;
    System.out.println("PendingRequests: " + sessId);

        try
		{
			wsdlURL = new URL("http://robinrmcomputer:8080/CheatApp-war/AssignmentManagementService?wsdl");
            QName serviceQName = new QName("HTTP://cheat.cs590.ipfw.edu","AssignmentManagementService");
			QName portQName =    new QName("HTTP://cheat.cs590.ipfw.edu","AssignmentManagementSOAP");
            Service service = null;
            service = Service.create(wsdlURL, serviceQName);

            AssignmentManagementInterface jiPort =
                    (AssignmentManagementInterface)  service.getPort(portQName,AssignmentManagementInterface.class);


			GetPendingRegistrationRequests ar = new GetPendingRegistrationRequests();

            GetPendingRegistrationRequestsResponse answer = jiPort.getPendingRegistrationRequests(ar, new Integer(sessId).toString());

            pendingUsers = answer.getGetPendingResult();

            System.out.println("Pending Users:");
            for (int i = 0; i < pendingUsers.size(); i++)
            {
                System.out.println("\t" + pendingUsers.get(i).getEmail());
            }
            System.out.println("_____________________");

		}
//        catch (AMSInsufficientAccessException_Exception ex)
//        {
//            System.out.println(ex.getMessage());
//        }
        catch (MalformedURLException e)
		{
			e.printStackTrace();
		}
        catch(Exception e)
        {
            System.out.println("EXCEPT: " + e.getMessage());
        }
	}



   private static void callRegistration(String user, String access) throws MalformedURLException
    {

        URL wsdlURL = null;
        System.out.println("Registration: " + user + "/" + access);

		try
		{
			wsdlURL = new URL("http://robinrmcomputer:8080/CheatApp-war/AssignmentManagementService?wsdl");
            QName serviceQName = new QName("HTTP://cheat.cs590.ipfw.edu","AssignmentManagementService");
			QName portQName =    new QName("HTTP://cheat.cs590.ipfw.edu","AssignmentManagementSOAP");
            Service service = null;
            service = Service.create(wsdlURL, serviceQName);

            AssignmentManagementInterface jiPort =
                    (AssignmentManagementInterface)  service.getPort(portQName,AssignmentManagementInterface.class);


            RegisteredPerson regUser = new RegisteredPerson();
            regUser.setAffiliation("The University of Relative Truth");
            regUser.setEmail(user);
            regUser.setFirstName("Sarcastic");
            regUser.setLastName("Bastard");
            regUser.setPhone("999.666.6669");
            regUser.setUrl("www.dotheworngthing.always");

            AccessRights aType = new AccessRights();
            //cheat.entities.AccessType a = new cheat.entities.AccessType();
            //String s = a.FACULTY_CODE;
            UserRights r = UserRights.INSTRUCTOR;
            aType.setRights(r);
            regUser.setRights(aType);
            regUser.setPassword(user);
            regUser.setUserId(user);

            boolean answer = jiPort.register(regUser);
            System.out.println(answer);
		}
        catch(SOAPFaultException e)
        {

        }
		catch (MalformedURLException e)
		{
			e.printStackTrace();
		}
        catch (Exception ex)
        {
            System.out.println(ex.getMessage() + " " + ex.getCause().getMessage() + ex.getCause().toString() );
        }

    }
}
